﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SecurityEvaluate;
using System.Data;
using Yipusoft.Account.Model;
using System.Globalization;
using System.Web.UI.DataVisualization.Charting;

public partial class Police_SecurityEvaluate_StatisticCombat : System.Web.UI.Page
{
    private string DepartmentID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Bind();

    }
    private void Bind()
    {
        for (int i = 2009; i < 2015; i++)
        {
            dptYear.Items.Add(new ListItem(i.ToString() + "年", i.ToString()));
        }
        for (int i = 1; i <= 12; i++)
        {
            dptMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
        }

        dptMonth.SelectedValue = DateTime.Now.Month.ToString();
        dptYear.SelectedValue = DateTime.Now.Year.ToString();
        BindChar();

    }

    private void BindChar()
    {
        if (Request["DepartmentID"] != null)
        {
            DepartmentID = Request["DepartmentID"];
            Chart1.Titles[0].Text = Yipusoft.Account.BLL.Department.GetDepartment(DepartmentID.Substring(0, 4) + "000").FullName + "士兵训练档案录入分析图";
        }
        else
        {
            DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        }
        //DepartmentID = "1301010";
        string strWhere = string.Empty;
        List<Job_Score> listScore = null;
        List<Job_Score> result = null;
        int subIndex = 0;
        if (DepartmentID.Substring(2, 3).Equals("000"))
        {
            subIndex = 4;
            listScore = Combat.GetJobScore(int.Parse(dptYear.SelectedValue), int.Parse(dptMonth.SelectedValue), "");
            strWhere = "Right(DepartmentID,3)='000' And DepartmentID<>'1300000'";

            LabelStyle lbl = new LabelStyle();
            System.Drawing.Font fnt = new System.Drawing.Font("Trebuchet MS", 8);
            lbl.Font = fnt;
            Chart1.ChartAreas[0].AxisX.LabelStyle = lbl;


        }
        else if (DepartmentID.Substring(4, 1).Equals("0"))
        {
            Chart1.Titles[0].Text = Yipusoft.Account.BLL.Department.GetDepartment(DepartmentID.Substring(0, 4) + "000").FullName + "士兵训练档案录入分析图";
            subIndex = 7;
            listScore = Combat.GetJobScore(int.Parse(dptYear.SelectedValue), int.Parse(dptMonth.SelectedValue), DepartmentID);
            strWhere = "Left(DepartmentID,4)='" + DepartmentID.Substring(0, 4) + "' And SubString(DepartmentID,5,1)='3'";

        }
        DataTable dtb = Account.ToDataTable(Yipusoft.Account.BLL.Department.GetList(strWhere));
        DataColumn cl = new DataColumn("InputScore", Type.GetType("System.Double"));
        cl.DefaultValue = 0;
        dtb.Columns.Add(cl);
        DataColumn cl2 = new DataColumn("FullScore", Type.GetType("System.Double"));
        cl2.DefaultValue = 0;
        dtb.Columns.Add(cl2);

        DataColumn cl3 = new DataColumn("FullCount", Type.GetType("System.Double"));
        cl3.DefaultValue = 0;
        dtb.Columns.Add(cl3);

        if (listScore == null)
            return;

        var groupresult = new object();
        for (int i = 0; i < dtb.Rows.Count; i++)
        {
            if (subIndex == 4)
            {
                result = listScore.Where(o => o.Department.Substring(0, subIndex) == dtb.Rows[i]["DepartmentID"].ToString().Substring(0, subIndex)).OrderBy(o => o.Sid).ToList();
                var byresult = result.GroupBy(o => o.Sid).ToList();

            }
            else
            {
                Yipusoft.SecurityEvaluate.FireBaseDataContext db = new FireBaseDataContext();
                var soldier = db.Soldiers.Where(o => o.DepartmentID == dtb.Rows[i]["DepartmentID"].ToString() && o.IsDelete == false && o.IsContract == false).ToList();

                result = (from obj in listScore where (from o in soldier select o.SoldierID.ToString()).Contains(obj.Sid) select obj).ToList();

                result = result.OrderBy(o => o.Sid).ToList();
            }

            var objresult = (from cc in result group cc by cc.Sid into g select g.Key).ToList();
            int tempcount = Combat.GetFullCount(dtb.Rows[i]["DepartmentID"].ToString(), int.Parse(dptYear.SelectedValue), int.Parse(dptMonth.SelectedValue), listScore);
            if (tempcount > 0)
            {
                dtb.Rows[i]["FullCount"] = tempcount;
                if (result == null)
                    return;
                for (int n = 0; n < objresult.Count(); n++)
                {

                    string sid = objresult[n].ToString();

                    if (result.Where(o => o.Sid == sid && o.FirstScoreHG != 0).Count() > 0)
                    {
                        //dtb.Rows[i]["InputScore"] = (dtb.Rows[i]["InputScore"] == null || dtb.Rows[i]["InputScore"] == "") ? 1 : int.Parse(dtb.Rows[i]["InputScore"].ToString()) + 1;
                        if (result.Where(o => o.Sid == sid && o.FirstScoreHG == 0).Count() > 0)
                        {
                            dtb.Rows[i]["FullScore"] = (dtb.Rows[i]["FullScore"] == null || dtb.Rows[i]["FullScore"] == "") ? 1 : int.Parse(dtb.Rows[i]["FullScore"].ToString()) + 1;
                        }


                    }
                    else
                    {
                        dtb.Rows[i]["InputScore"] = (dtb.Rows[i]["InputScore"] == null || dtb.Rows[i]["InputScore"] == "") ? 1 : int.Parse(dtb.Rows[i]["InputScore"].ToString()) + 1;
                    }


                }
            }
            else
            {
                dtb.Rows.Remove(dtb.Rows[i]);
            }

        }

        DataTable dtbvw = dtb.Clone();
        for (int i = 0; i < dtb.Rows.Count; i++)
        {


            int fullcount = int.Parse(dtb.Rows[i]["FullCount"] == null ? "0" : dtb.Rows[i]["FullCount"].ToString());
            if (fullcount <= 0 || dtb.Rows[i]["FullCount"] == null || dtb.Rows[i]["FullCount"].ToString() == "0.0")
            {
                // dtb.Rows.Remove(dtb.Rows[i]);
            }
            else
            {


                int fullscore = int.Parse(dtb.Rows[i]["InputScore"] == null ? "1" : dtb.Rows[i]["InputScore"].ToString());
                int count = int.Parse(dtb.Rows[i]["FullScore"] == null ? "0" : dtb.Rows[i]["FullScore"].ToString());

                dtb.Rows[i]["InputScore"] = double.Parse(((double)fullscore / (double)fullcount).ToString("F2"));
                dtb.Rows[i]["FullScore"] = double.Parse(((double)count / (double)fullcount).ToString("F2"));
                dtbvw.Rows.Add(dtb.Rows[i].ItemArray);
            }

        }
        DataView dv = dtbvw.DefaultView;
        dv.Sort = "InputScore desc";
        Chart1.Series["未录入率"].Points.DataBindXY(dv, "ShortName", dv, "InputScore");
        Chart1.Series["录入不完整性"].Points.DataBindXY(dv, "ShortName", dv, "FullScore");
        if (DepartmentID.Substring(2, 3).Equals("000"))
        {
            for (int i = 0; i < dv.Table.Rows.Count; i++)
            {
                Chart1.Series["未录入率"].Points[i].Url = "StatisticCombat.aspx?DepartmentID=" + dv[i]["DepartmentID"];
                Chart1.Series["录入不完整性"].Points[i].Url = "StatisticCombat.aspx?DepartmentID=" + dv[i]["DepartmentID"];
            }
        }
    }

    protected void lbtnQuery_Click(object sender, EventArgs e)
    {
        BindChar();
    }
}
